<?php
/********************
 *@param function:訂單管理 model
 *@param datetime 2011-6-7
 *@param author abin
 ********************/
class Orders_mdl extends CI_model{
  //操作的表.
  const TAB_NAME='clickrcms_order';
  const PAY_TAB_NAME = "clickrcms_pays";
  function __construct(){
    parent::__construct();
    log_message('debug','Orders_mdl Model class Initialized');
  }

  /****
   *查詢所orders,加分页
   *$data :查询参数.
   */
  public function get_orders($data=array()){
    if(!empty($data)){
      extract($data);
      isset($field) && isset($sort)?$this->db->order_by($field.' '.$sort):$this->db->order_by('add_time DESC');
      isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
      isset($id) && !empty($id)?$this->db->where('id',$id):'';
      isset($status) && !empty($status)?$this->db->where('status',$status):'';
      isset($is_print) && !empty($is_print)?$this->db->where('is_print',$is_print):'';
      isset($id_array) && !empty($id_array)?$this->db->where_in('id',$id_array):'';
      isset($order_status_id) && !empty($order_status_id)?$this->db->where('order_status_id',$order_status_id):'';
      isset($nickname) && !empty($nickname)?$this->db->like('nickname',$nickname):'';
      isset($phone) && !empty($phone)?$this->db->like('phone',$phone):'';
      isset($invoice_no) && !empty($invoice_no)?$this->db->like('invoice_no',$invoice_no):'';
      isset($begin_time) && isset($end_time)?$this->db->where('add_time BETWEEN "'.$begin_time.'" AND "'.$end_time.'"',NULL,FALSE):'';//index
      return $this->db->get(self::TAB_NAME);
    }
  }
  public function get_order_ids($condition=null){
    if(!empty($condition)){
       $this->db->where($condition);
    }
    $this->db->select('id,add_time');
    $query = $this->db->get(self::TAB_NAME);
    $data = array();
    $results = $query->result_array();
    if($results){
        foreach($results as $result){
         $data[$result['id']] = date('Y-m-d',$result['add_time']);
        }
    }
    return $data;
  }
  public function get_order_products($condition=null){
      $data = $this->get_order_ids($condition);
      if(empty($data)){
         return array();
      }
      $orderIDs = array_keys($data);
      $this->db->where_in('order_id',$orderIDs);
      $this->db->select('id,order_id,pname,product_id');
      $query = $this->db->get('clickrcms_order_product');
      $results = $query->result_array();
      $base = array();
      $prodcuts = array();
      foreach($results as $item){
        if(!isset($base[$data[$item['order_id']]][$item['product_id']])){
            $base[$data[$item['order_id']]][$item['product_id']] =0;
            $prodcuts[$item['product_id']] = $item['pname'];
        }
        $base[$data[$item['order_id']]][$item['product_id']]+=1;
        
      }
      return array('prodcuts'=>$prodcuts,'base'=>$base);
  }
  /****
   * 統計所有的orders
   */
  public function totle_orders($data=array()){
    if(!empty($data)){
      extract($data);
      $this->db->order_by('add_time DESC');
      isset($status) && !empty($status)?$this->db->where('status',$status):'';
      isset($is_print) && !empty($is_print)?$this->db->where('is_print',$is_print):'';
      isset($order_status_id) && !empty($order_status_id)?$this->db->where('order_status_id',$order_status_id):'';
      isset($nickname) && !empty($nickname)?$this->db->like('nickname',$nickname):'';
      isset($phone) && !empty($phone)?$this->db->like('phone',$phone):'';
      isset($invoice_no) && !empty($invoice_no)?$this->db->like('invoice_no',$invoice_no):'';
      isset($begin_time) && isset($end_time)?$this->db->where('add_time BETWEEN "'.$begin_time.'" AND "'.$end_time.'"',NULL,FALSE):'';//index
      return $this->db->count_all_results(self::TAB_NAME);
    }else{
      return $this->db->count_all(self::TAB_NAME);
    }
  }
  /*****
   * 根據ID來刪除
   */
  public function delete_orders($delete_id=0){
    if(!empty($delete_id)){
      $this->db->where("order_id",$delete_id);
      $this->db->delete('clickrcms_order_product');
      $this->db->where("id",$delete_id);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /*****
   * 根據ID數組來刪除 批量刪除
   */
  public function delete_batch($batch=array()){
    if(!empty($batch)){
      $this->db->where_in("order_id",$batch);
      $this->db->delete('clickrcms_order_product');
      $this->db->where_in("id",$batch);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /****
   *根据ID来查询得到一个信息.
   *$id :要查詢的ID
   */
  public function get_orders_by_id($id){
    $data=array();
    $this->db->where('id',$id);
    $get_result=$this->db->get(self::TAB_NAME);
    if($get_result->num_rows()>0){
      $data=$get_result->row_array();
    }
    $get_result->free_result();
    return $data;
  }

  /****
   *根据ID来查询得到一个信息.
   *$id :要查詢的ID
   */
  public function get_one_order($filter_item=array()){
    if(!empty($filter_item) && is_array($filter_item)){
      $item=array();
      $this->db->where($filter_item);
      $query=$this->db->get(self::TAB_NAME);
      if($query->num_rows()>0){
        $item=$query->row_array();
      }
      $query->free_result();
      return $item;
    }
  }
  /***
   * 修改一個orders
   * @param array $data
   * @param int  $c_id
   * return :受影响的行数
   */
  public function edit_orders($data=array(),$b_id=0){
    if(!empty($data) && !empty($b_id)){
      $this->db->where('id',$b_id);
      $this->db->update(self::TAB_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /***
   * 新增一個 orders
   */
  public function add_orders($data=array()){
    if(!empty($data)){
      $this->db->insert(self::TAB_NAME,$data);
      return $this->db->insert_id();
    }
  }
  /*獲取支付方式*/
  public function get_payments($condition=array()){
    if($condition){
      extract($condition);
      isset($status) && !empty($status)?$this->db->where('status',$status):'';
    }
    $this->db->order_by('sort asc');  
    $query = $this->db->get(self::PAY_TAB_NAME);
    return $query->result();

  }
  public function update_payment($code,$data){
    $this->db->where('code',$code);
    $this->db->update(self::PAY_TAB_NAME,$data);
    return ($this->db->affected_rows()>0)?true:false;
  }
  public function get_one_payment($code){
      $item=array();
      $this->db->where('code',$code);
      $query=$this->db->get(self::PAY_TAB_NAME);
      if($query->num_rows()>0){
        $item=$query->row_array();
      }
      $query->free_result();
      return $item;
  }
}